package com.wntms.wy.web;

import com.github.pagehelper.PageInfo;
import com.wntms.wy.pojo.ErrorInformation;
import com.wntms.wy.pojo.Result;
import com.wntms.wy.pojo.sjhz.DriverBill_s;
import com.wntms.wy.service.ErrorInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/error")
@CrossOrigin
public class ErrorInformationController {
    @Autowired
    private ErrorInformationService errorInformationService;
    @GetMapping("/panduanError/{fid}")
    public Result panduanError(@PathVariable String fid){
        Boolean aBoolean1 = errorInformationService.panduanFCByFid(fid);

        Boolean aBoolean = errorInformationService.puanduanIspass(fid);
        //判断是已经验收false代表没有填写过差错信息
        Boolean ab2 = errorInformationService.panduanEIByFid(fid);
        Result result = new Result();
        if (!aBoolean1){
            result.setCode(500);
            result.setMsg("没有这个货运单，不能填写差错信息！");
            return result;
        }
        if(!ab2){
            //表示没有填写过fid的差错信息
            if (aBoolean){
                //说明业务员验收或客户验收有一个不通过，
                result.setCode(200);
                result.setMsg("可以继续填写差错信息。");
            }else {
                //增加失败
                result.setCode(500);
                result.setMsg("前面验收正常，不能填写差错信息！");
            }
        }else {
            result.setCode(500);
            result.setMsg("已经填写过差错信息，不能再填写！");
        }

        return result;
    }
    @PostMapping
    public Result addEI(@RequestBody ErrorInformation errorInformation){
        Boolean aBoolean = errorInformationService.addErrorInformation(errorInformation);
        Result result = new Result();
        if (aBoolean){
            //增加成功
            result.setCode(200);
            result.setMsg("增加成功");
        }else {
            //增加失败
            result.setCode(500);
            result.setMsg("增加失败");
        }
        return result;
    }
    @DeleteMapping("/{id}")
    public Result deleteEI(@PathVariable Integer id){
        Boolean aBoolean = errorInformationService.deleteErrorInformation(id);
        Result result = new Result();
        if (aBoolean){
            //删除成功
            result.setCode(200);
            result.setMsg("删除成功");
        }else {
            //删除失败
            result.setCode(500);
            result.setMsg("删除失败");
        }
        return result;
    }
    @PutMapping
    public Result updateEI(@RequestBody ErrorInformation errorInformation){
        Boolean aBoolean = errorInformationService.updateErrorInformation(errorInformation);
        Result result = new Result();
        if (aBoolean){
            //修改成功
            result.setCode(200);
            result.setMsg("修改成功");
        }else {
            //修改失败
            result.setCode(500);
            result.setMsg("修改失败");
        }
        return result;
    }
    @GetMapping("/{currentPage}/{pageSize}")
    public Result findAllEI(@PathVariable Integer currentPage,@PathVariable Integer pageSize){
        List<ErrorInformation> list = errorInformationService.findAllErrorInformaton(currentPage,pageSize);
        Result result = new Result();
        if (list!=null){
            //查询成功
            PageInfo<ErrorInformation> pageInfo = new PageInfo<>(list);
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(pageInfo);
        }else {
            //查询失败
            result.setCode(500);
            result.setMsg("查询失败");
        }
        return result;
    }
    @PostMapping("/upLoadImg")
    public Result upLoadImg(MultipartFile file){
        String name = file.getOriginalFilename();

        System.out.println(name);
        return null;
    }
}
